<template>
  <div class="list" ref="wrapper">
    <div>
      <div class="area">
        <div class="title border-topbottom">当前城市</div>
        <div class="button-list">
          <div class="button-wrapper">
            <div class="button">{{ this.$store.state.city }}</div>
          </div>
        </div>
      </div>
      <div class="area">
        <div class="title border-topbottom">热门城市</div>
        <div class="button-list">
          <div class="button-wrapper">
            <div class="button" v-for="item of hot" :key="item.id" @click="handleCityClidk(item.name)">{{ item.name }}</div>
          </div>
        </div>
      </div>
      <div class="area" v-for="(item, key) of cities" :key="key" :ref="key">
        <div class="title border-topbottom">{{ key }}</div>
        <div class="item-list">
          <div v-for="innerItem of item" :key="innerItem.id" @click="handleCityClidk(innerItem.name)">{{ innerItem.name }}</div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import BScoll from 'better-scroll'
export default {
  name: 'CityList',
  props: {
    hot: Array,
    cities: Object,
    letter: String
  },
  data () {
    return {
      city: ''
    }
  },
  methods: {
    handleCityClidk (city) {
      // 调用store的dispatch方法去触发Actions，并把值传过去。
      // 派发一个名字为 changeCity 的 Actions，然后把city传过去
      // this.$store.dispatch('changeCity', city) // 当没有异步操作或者不是批量操作数据时，组件也可以不通过Actions直接commit到Mutations
      this.$store.commit('changeCity', city)
      this.$router.push({ path: '/' })
    }
  },
  mounted () {
    // console.log(this.$refs.wrapper)
    // better-scroll接收一个DOM节点，节点可通过$refs获取
    this.scroll = new BScoll(this.$refs.wrapper)
  },
  watch: {
    letter () {
      if (this.letter) {
        const element = this.$refs[this.letter][0]
        this.scroll.scrollToElement(element)
      }
    }
  }
}
</script>

<style lang="stylus" scoped>
  .border-topbottom
    &:before
      border-color: #ccc
    &:after
      border-color: #ccc
  .border-bottom
    &:before
      border-color: #ccc
  .list
    overflow: hidden
    position: absolute
    top: 83px
    left: 0
    right: 0
    bottom: 0
    .title
      line-height: 20px
      background: #eee
      padding-left: 20px
      color: #666
      font-size: 14px
    .button-list
      overflow: hidden
      padding: .1rem .6rem .1rem .1rem
      .button-wrapper
        float: left
        width: 100%
        .button
          margin: .1rem
          padding: .1rem 0
          text-align: center
          border: 1px solid #ccc
          border-radius: .06rem
          width: 30%
          float: left
    .item-list
      div
        padding: 8px 10px
        border-bottom: 1px solid #ddd
</style>
